package com.hanj.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 解决跨域问题的配置类
 *
 * @author hanj.cn@outlook.com
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configuration = new CorsConfiguration();

        // 允许任意请求头跨域
        configuration.addAllowedHeader("*");
        // 允许任意请求方式跨域
        configuration.addAllowedMethod("*");
        // 任意来源都允许跨域
        configuration.addAllowedOrigin("*");
        // 允许携带cookie跨域
        configuration.setAllowCredentials(true);

        // "/**" --> 任意路径都需要跨域
        source.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(source);
    }

}
